package com.situ.company.employee.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.employee.dao.IEmployeeDao;
import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

public class EmployeeDaoImpl implements IEmployeeDao {

	public Integer insert(EmployeeModel model) {
		String sql = "insert into employee(code,name,pass)values(?,?,?)";
		return JDBCUtil.update(sql, model.getCode(), model.getName(), model.getPass());
	}

	public Integer updateActive(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("update employee set id=id ");
		List<Object> vals = appendUpdate(sql, model);
//		System.out.println(sql.toString());
//		for (int i = 0; i < vals.size(); i++) {
//			System.out.println(vals.get(i));
//		}
		return JDBCUtil.update(sql.toString(), vals);
	}
	
	/**
	 * 拼接更新内容
	 * @param sql
	 * @param model
	 * @return
	 */
	public List<Object> appendUpdate(StringBuffer sql,EmployeeModel model){
		List<Object> vals=new ArrayList<>();
//		String code=model.getCode();
//		if(code!=null) {
//			vals.add(code);
//			sql.append(",code=?");
//		}
		String name=model.getName();
		if(name!=null) {
			vals.add(name);
			sql.append(",name=?");
		}
		String pass=model.getPass();
		if(pass!=null) {
			vals.add(pass);
			sql.append(",pass=?");
		}
		String codeDept=model.getCodeDept();
		if(codeDept!=null) {
			vals.add(codeDept);
			sql.append(",code_dept=?");
		}
		String image=model.getImage();
		if(image!=null) {
			vals.add(image);
			sql.append(",image=?");
		}
		sql.append(" where code=?");
		vals.add(model.getCode());
		return vals;
	}

	public Integer updateAll(EmployeeModel model) {
		String sql = "update employee set name=?,pass=? where code=?";
		return JDBCUtil.update(sql, model.getName(), model.getPass(), model.getCode());
	}

	public Integer deleteBycode(EmployeeModel model) {
		String sql = "delete from employee where code=?";
		return JDBCUtil.update(sql, model.getCode());
	}

	public Integer delete(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("delete from employee ");
		List<Object> vals = appendWhere(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}

	public EmployeeModel selectModel(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("select code,name,pass,code_dept,image from employee");
		List<Object> vals = appendWhere(sql, model);
		Map<String, String> props = new HashMap<>();
		props.put("code", "code");
		props.put("name", "name");
		props.put("pass", "pass");
		props.put("codeDept", "code_dept");
		props.put("image", "image");
		
//		System.out.println(sql.toString());
		
		return JDBCUtil.queryModel(sql.toString(), vals, EmployeeModel.class, props);
	}

	public List<EmployeeModel> selectList(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("select code,name,pass,code_dept,image,(select name from department where code=code_dept) nameDept from employee");
		List<Object> vals = appendWhere(sql, model);
//		List<String> props=Arrays.asList("code","name","pass");
		Map<String, String> props = new HashMap<>();
		props.put("code", "code");
		props.put("name", "name");
		props.put("pass", "pass");
		props.put("codeDept", "code_dept");
		props.put("nameDept", "nameDept");
		props.put("image", "image");
	
		return JDBCUtil.queryList(sql.toString(), vals, EmployeeModel.class, props);
	}

	public Integer selectCount(EmployeeModel model) {
		StringBuffer sql=new StringBuffer("select count(1) from employee");
		List<Object> vals=appendWhere(sql, model);
		return JDBCUtil.queryInt(sql.toString(), vals);
	}

	public List<Object> appendWhere(StringBuffer sql, EmployeeModel model) {
		List<Object> vals = new ArrayList<>();
		sql.append(" where 1=1 ");
		String code = model.getCode();
		if (!FmtEmpty.isEmpty(code)) {
			sql.append(" and code like ? ");
			vals.add(code);
		}
		String name = model.getName();
		if (!FmtEmpty.isEmpty(name)) {
			sql.append(" and name like ? ");
			vals.add(name);
		}
		String CodeDept = model.getCodeDept();
		if (!FmtEmpty.isEmpty(CodeDept)) {
			sql.append(" and code_dept like ? ");
			vals.add(CodeDept);
		}
		String image = model.getImage();
		if (!FmtEmpty.isEmpty(image)) {
			sql.append(" and image like ? ");
			vals.add(image);
		}
		if(model.isPageOn()) {
			//1-0,10
			//2-10,10
			//3-20,10
			sql.append(" limit ?,?");
			vals.add(model.getRowStart());
			vals.add(model.getPageLimit());
		}
		return vals;

	}

}
